package com.young.shop_admin.services.impl;

import com.young.shop_admin.dao.GoodsDao;
import com.young.shop_admin.dao.OrderDao;
import com.young.shop_admin.dao.SellerDao;
import com.young.shop_admin.dao.impl.GoodsDaoImpl;
import com.young.shop_admin.dao.impl.OrderDaoImpl;
import com.young.shop_admin.dao.impl.SellerDaoImpl;
import com.young.shop_admin.pojo.qo.PaymentQO;
import com.young.shop_admin.pojo.vo.ManagementCenterDataVO;
import com.young.shop_admin.services.ManagementCenterService;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class ManagementCenterServiceImpl implements ManagementCenterService {
    private GoodsDao goodsList = new GoodsDaoImpl();
    private SellerDao sellerList = new SellerDaoImpl();
    private OrderDao orderList = new OrderDaoImpl();

    @Override
    public ManagementCenterDataVO getInitData() {
        //获取商品总数
        int totalCount = 0;
        try {
            totalCount = goodsList.findTotalCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //获取在售商品数
        int onSaleGoodsNumber = goodsList.getOnSaleGoodsNumber();
        System.out.println(onSaleGoodsNumber);

        //获取当前时间
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();

        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        dBefore = calendar.getTime();   //得到前一天的时间

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd "); //设置时间格式
        String defaultStartDate = sdf.format(dBefore);    //格式化前一天
        String defaultEndDate = sdf.format(dNow); //格式化当前时间

        //获取昨日新增用户数量
        int addUserNumber = sellerList.getAddUserNumber(defaultStartDate, defaultEndDate);
        System.out.println(addUserNumber);

        //获取昨日交易总和
        PaymentQO DBAddPayment = orderList.getAddPayment(defaultStartDate, defaultEndDate);
        int addPayment = DBAddPayment.getPayment();

        ManagementCenterDataVO managementCenterData = new ManagementCenterDataVO();
        managementCenterData.setTotalGoodsNumber(totalCount);
        managementCenterData.setOnSaleGoodsNumber(onSaleGoodsNumber);
        managementCenterData.setAddUserNumber(addUserNumber);
        managementCenterData.setAddPayment(addPayment);

        return managementCenterData;
    }
}
